#include "ChatService.hpp"
#include "public.hpp"
#include <muduo/base/Logging.h>
// 把实例对象写为静态类型这样子还不会有线程安全的问题
ChatService *ChatService::instance()
{
    // 调用构造函数生成唯一实例
    static ChatService megType;
    // 返回static对象
    return &megType;
}

handler ChatService::getHandler(int messageid)
{
    auto it = _MegHandlerMap.find(messageid);
    // 关于这个怎么处理的等下再看看
    if (it == _MegHandlerMap.end())
    {
        // 下面是我的处理方法 ， 这种处理方法有点挫了
        //    LOG_ERROR << "The messageid is not find in MessageHandlerMap";
        //    return;
        // 使用lambda表达式处理
        return [=](const muduo::net::TcpConnectionPtr &conn, json& js, muduo::Timestamp time)
        {
            LOG_ERROR << "The messageid is : " << messageid << " not find in MessageHandlerMap";
        };
    }

    return _MegHandlerMap[messageid];
}

ChatService::ChatService()
{
    //_MegHandlerMap.insert(std::make_pair(LOGIN_MSG, std::bind(&ChatService::Login, this, _1, _2, _3)));
    //_MegHandlerMap.insert(std::make_pair(REG_MSG, std::bind(&ChatService::Register, this, _1, _2, _3)));

    _MegHandlerMap.insert({LOGIN_MSG, std::bind(&ChatService::Login, this, _1, _2, _3)});
    _MegHandlerMap.insert({REG_MSG, std::bind(&ChatService::Register, this, _1, _2, _3)});
}

void ChatService::Login(const muduo::net::TcpConnectionPtr &conn, json& js, muduo::Timestamp time)
{
    LOG_INFO << "do Login handler";
}

void ChatService::Register(const muduo::net::TcpConnectionPtr &conn, json& js, muduo::Timestamp time)
{
    LOG_INFO << "do Register handler";
}